package cn.life.business.user.controller;

import cn.life.business.material.entity.po.MaterialPo;
import cn.life.business.user.entity.vo.LoginUserVo;
import cn.life.business.user.entity.vo.UserInfoVo;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.model.MyUserInfo;
import cn.life.common.entity.model.MyUser;
import cn.life.business.user.entity.po.LoginUser;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.business.user.entity.vo.RegisterUserVo;
import cn.life.business.user.service.UserService;
import cn.life.common.entity.po.ProjectPo;
import cn.life.common.support.check.UserInterceptorHandler;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Description:    用户信息控制层
 *
 * @Author 李佳明
 * @Date 2018/6/14 14:01
 */
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * @Description:    注册用户
     *
     * @param user
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/register")
    public CMessage registerUser(@RequestBody RegisterUserVo user,@MyUser MyUserInfo myUserInfo){
        copyUserInfo(user,myUserInfo);
        return userService.registerUser(user);
    }

    /**
     * @Description:    用户登陆
     *
     * @return:
     *
     * @Author 李佳明
     * @Date  2018/7/16 0016 上午 9:46
     */
    @PostMapping(value = "/mobile/login")
    public CMessage login(HttpServletRequest request, HttpServletResponse response, @RequestBody LoginUser loginUser) throws IOException {
        logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Param Start<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        logger.debug("URL : mobile/login" );
        logger.debug("Param : loginName" + loginUser.getLoginName() + ";loginPassword:" + loginUser.getLoginPassword() + ";JgPushMark:" + loginUser.getJgPushMark() + ";HwPushMark:" + loginUser.getHwPushMark() + ";IosPushMark:" + loginUser.getIosPushMark());
        logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Param End<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        return userService.login(request,response,loginUser);
    }

    /**
     * @Description:    修改用户信息（登陆情况下）
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/16 0016 上午 9:44
     */
    @PostMapping(value = "/update")
    public CMessage updateUser(@RequestBody UserInfo userInfo){
        return userService.updateUser(userInfo);
    }

    /**
     * @Description:    删除用户信息（管理员权限下）
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/16 0016 上午 9:44
     */
    @PostMapping(value = "/delete")
    public CMessage deleteUser(@RequestBody UserInfo userInfo){
        return userService.deleteUser(userInfo);
    }


    /**
     * @Description:    获取用户列表
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/getUserList")
    public CMessage getUserList(@RequestBody UserInfo userInfo, @MyUser MyUserInfo myUserInfo){
        userInfo.setFactoryId(myUserInfo.getFactoryId());
        return userService.getUserList(userInfo);
    }

    /**
     * @Description:    获取项目下的用户列表
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/getProUserList")
    public CMessage getProUserList(@RequestBody UserInfo userInfo, @MyUser MyUserInfo myUserInfo){
        userInfo.setFactoryId(myUserInfo.getFactoryId());
        return userService.getProUserList(userInfo);
    }

    /**
     * @Description:    获取用户信息
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/getUserInfo")
    public CMessage getUserInfo(@RequestBody UserInfo userInfo, @MyUser MyUserInfo myUserInfo){
        if(userInfo == null || userInfo.getUserId() == null){
            BeanUtils.copyProperties(myUserInfo,userInfo);
        }
        return userService.getUserInfo(userInfo);
    }

    /**
     * @Description:    获取用户信息(管理员)
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/queryDetail")
    public CMessage queryDetail(@RequestBody UserInfo userInfo, @MyUser MyUserInfo myUserInfo){
        userInfo.setFactoryId(myUserInfo.getFactoryId());
        return userService.queryDetail(userInfo);
    }

    /**
     * @Description:    修改用户权限与账号信息
     *
     * @param userInfo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/7/5 0005 6:51
     */
    @PostMapping(value = "/updateRole")
    public CMessage updateUserRole(@RequestBody UserInfoVo userInfo, @MyUser MyUserInfo myUserInfo){
        copyUserInfo(userInfo,myUserInfo);
        return userService.updateUserRole(userInfo);
    }

    /**
     * @Description:    修改密码
     *
     * @param loginUserVo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018-09-03 上午 10:36
     */
    @PostMapping(value = "/updatePassword")
    public CMessage updatePassword(@RequestBody LoginUserVo loginUserVo, @MyUser MyUserInfo myUserInfo){
        if(myUserInfo == null && (loginUserVo.getLoginName() == null || loginUserVo.getLoginName().equals(""))){
            return CMessage.fail("请先输入账号信息");
        }
        if(myUserInfo == null && (loginUserVo.getOldPassword() == null || loginUserVo.getOldPassword().equals(""))){
            return CMessage.fail("请输入原有密码");
        }
        if(loginUserVo.getLoginPassword() == null || loginUserVo.getLoginPassword().equals("")){
            return CMessage.fail("请输入新密码");
        }
        return userService.updatePassword(loginUserVo,myUserInfo);
    }

    /**
     * @Description:    根据项目Id查询,除了自己与技术工长
     *
     * @param projectPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/12/1 0001 9:53
     */
    @PostMapping(value = "/materialAuditUserList")
    public CMessage queryMaterialAuditUserList(@MyUser MyUserInfo myUserInfo,@RequestBody ProjectPo projectPo){
        if(projectPo.getProjectId() == null){
            projectPo.setProjectId(myUserInfo.getProjectId());
        }
        projectPo.setOprUserId(myUserInfo.getUserId());
        return userService.queryMaterialAuditUserList(projectPo);
    }

    /**
     * @Description:    根据项目Id查询总经理，董事长
     *
     * @param projectPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/12/1 0001 9:53
     */
    @PostMapping(value = "/scheduleAuditUserList")
    public CMessage queryScheduleAuditUserList(@MyUser MyUserInfo myUserInfo,@RequestBody ProjectPo projectPo){
        if(projectPo.getProjectId() == null){
            projectPo.setProjectId(myUserInfo.getProjectId());
        }
        projectPo.setOprUserId(myUserInfo.getUserId());
        return userService.queryScheduleAuditUserList(projectPo);
    }

    /**
     * @Description:    根据项目Id查询技术工长
     *
     * @param projectPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/12/1 0001 9:53
     */
    @PostMapping(value = "/technologyManagerList")
    public CMessage queryTechnologyManagerList(@MyUser MyUserInfo myUserInfo,@RequestBody ProjectPo projectPo){
        if(projectPo.getProjectId() == null){
            projectPo.setProjectId(myUserInfo.getProjectId());
        }
        projectPo.setOprUserId(myUserInfo.getUserId());
        return userService.queryTechnologyManagerList(projectPo);
    }

    /**
     * @Description:    yuan
     *
     * @param projectPo
     * @return:
     *
     * @Author 李佳明
     * @Date 2018/12/1 0001 9:53
     */
    @PostMapping(value = "/statistics")
    public CMessage queryUserStatistics(@MyUser MyUserInfo myUserInfo,@RequestBody ProjectPo projectPo){
        if(projectPo.getProjectId() == null){
            projectPo.setProjectId(myUserInfo.getProjectId());
        }
        return userService.queryUserStatistics(projectPo);
    }

    public void copyUserInfo(RegisterUserVo userInfo,MyUserInfo myUserInfo){
        userInfo.setFactoryId(myUserInfo.getFactoryId());
        userInfo.setOprUserId(myUserInfo.getUserId());
        userInfo.setOprUserName(myUserInfo.getUserName());
        userInfo.setOprDate(new Date());
    }

    public void copyUserInfo(UserInfoVo userInfo,MyUserInfo myUserInfo){
        userInfo.setFactoryId(myUserInfo.getFactoryId());
        userInfo.setOprUserId(myUserInfo.getUserId());
        userInfo.setOprUserName(myUserInfo.getUserName());
        userInfo.setOprDate(new Date());
    }


}
